﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.QuanTri
{
    public partial class frmNguoiDungDetail : Form
    {
        private bool _isEditForm;
        private NguoiDung _nguoiDung;
        private int _nguoiDungId;
        private frmDsNguoiDung _callerForm;

        public int NguoiDungId
        {
            set
            {
                _nguoiDungId = value;
                //reload NguoiDung
                _nguoiDung = NguoiDungBO.getInstance.GetNguoiDungById(_nguoiDungId);
                RefreshForm();
            }
        }

        public bool IsEditForm
        {
            set
            {
                _isEditForm = value;
                if (!_isEditForm)
                    FillFormNguoiDung();
            }
        }

        public frmDsNguoiDung CallerForm
        {
            get { return _callerForm; }
            set { _callerForm = value; }
        }

        private void FillCboVaiTro()
        {
            // Fill combobox vai tro
            cboVaiTro.DataSource = NguoiDungBO.getInstance.GetAllVaiTro();
            cboVaiTro.DisplayMember = "Ten";
            cboVaiTro.ValueMember = "Id";
        }
        public frmNguoiDungDetail()
        {
            InitializeComponent();
            _isEditForm = false;
            _nguoiDungId = 0;
            _nguoiDung = null;
            FillCboVaiTro();
        }

        private void FillFormNguoiDung()
        {
            if (_isEditForm)
            {
                txtUsername.Text = _nguoiDung.Username;
                txtPwd.Text = _nguoiDung.Password;
                txtGhiChu.Text = _nguoiDung.GhiChu;
                if (_nguoiDung.TrangThai == 1)
                {
                    chkActive.Checked = true;
                } 
                else
                {
                    chkActive.Checked = false;
                }
                cboVaiTro.Text = _nguoiDung.VaiTro.Ten;
            } 
            else
            {
                this.Text = "Thêm người dùng";
                txtTitle.Text = "Thêm mới người dùng";
                chkActive.Checked = true;
                txtPwd.Text = "";
                txtUsername.Text = "";
                txtGhiChu.Text = "";
            }
        }

        private bool IsDataHasChanged()
        {
            bool hasChange = false;
            //only check in case edit
            if(_isEditForm && _nguoiDung != null)
            {
                int TrangThai = 0;
                if (chkActive.Checked)
                {
                    TrangThai = 1;
                } 
                if (txtPwd.Text != _nguoiDung.Password
                   || txtUsername.Text != _nguoiDung.Username
                   || txtGhiChu.Text != _nguoiDung.GhiChu
                   || TrangThai != (int)_nguoiDung.TrangThai
                   || (int)cboVaiTro.SelectedValue != (int)_nguoiDung.VaiTroId)
                {
                    hasChange = true;
                }
            }
            else
            {
                if (txtPwd.Text != "" || txtUsername.Text != "" || txtGhiChu.Text != "")
                {
                    hasChange = true;
                }
            }
            return hasChange;
        }

        private bool SaveNguoiDung()
        {
            if (ValidateFormData())
            {
                if (IsDataHasChanged())
                {
                    int TrangThai = 0;
                    if (chkActive.Checked)
                    {
                        TrangThai = 1;
                    }
                    if (_isEditForm)
                    {
                        //update  khach hang
                        _nguoiDung.Password = txtPwd.Text.Trim();
                        _nguoiDung.Username= txtUsername.Text.Trim();
                        _nguoiDung.GhiChu = txtGhiChu.Text.Trim();
                        _nguoiDung.TrangThai = TrangThai;
                        _nguoiDung.VaiTroId = (int)cboVaiTro.SelectedValue;
                        NguoiDungBO.getInstance.UpdateNguoiDung(_nguoiDung);
                    } 
                    else
                    {
                        //insert khach hang
                        _nguoiDung = new NguoiDung();
                        _nguoiDung.Password = txtPwd.Text.Trim();
                        _nguoiDung.Username= txtUsername.Text.Trim();
                        _nguoiDung.GhiChu = txtGhiChu.Text.Trim();
                        _nguoiDung.TrangThai = TrangThai;
                        _nguoiDung.VaiTroId = (int)cboVaiTro.SelectedValue;
                        NguoiDungBO.getInstance.InsertNguoiDung(_nguoiDung);
                        _callerForm.RefreshForm();
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ValidateFormData()
        {
            //hien thi message du lieu ko valid
            return true;
        }

        private void RefreshForm()
        {
            FillFormNguoiDung();
        }

        private void frmUserDetail_Load(object sender, EventArgs e)
        {
            //Neu la edit thi load form edit khong thi load form them moi
            if (_isEditForm)
            {
                if(_nguoiDung == null)
                {
                    return;
                }
            }
 
            FillFormNguoiDung();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveNguoiDung())
            {
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(IsDataHasChanged())
            {
                if (MessageBox.Show("Dữ liệu có sự thay đổi, Bạn có muốn lưu không?"
                , "Đóng", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //save data
                    if (SaveNguoiDung())
                    {
                        this.Close();
                    }
                }
                else
                {
                    //close form
                    this.Close();    
                }
            }
            else
            {
                //close form
                this.Close();
            }
        }
    }
}
